package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("checkgroup")
@Slf4j
public class CheckGroupController {
    @DubboReference
    private CheckGroupService checkGroupService;

    /**
     * @Author: 位帅帅
     * @Date: 14:35 2021/1/5
     * @Params: [queryPageBean]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 分页查询检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]param: {}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        log.info("[检查组-分页查询]result: {}",pageResult);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }

    /**
     * @Author: 位帅帅
     * @Date: 15:47 2021/1/5
     * @Params: [checkitemIds, checkGroup]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 添加检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @PostMapping("add")
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        log.info("[检查组-添加]checkitemIds: {}, checkGroup: {}",checkitemIds,checkGroup);
        checkGroupService.add(checkitemIds,checkGroup);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * @Author: 位帅帅
     * @Date: 16:48 2021/1/5
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 根据id查询检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("[检查组-id查询]id: {}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        log.info("[检查组-id查询]checkGroup: {}",checkGroup);
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * @Author: 位帅帅
     * @Date: 17:06 2021/1/5
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 根据检查组id查询所有检查项id
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查组-根据检查组id查询所有检查项id]id: {}",id);
        List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        log.info("[检查组-根据检查组id查询所有检查项id]checkItemIds: {}",checkItemIds);
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkItemIds);
    }

    /**
     * @Author: 位帅帅
     * @Date: 17:40 2021/1/5
     * @Params: [checkitemIds, checkGroup]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 编辑检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @PostMapping("edit")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup) {
        log.info("[检查组-编辑]checkitemIds: {}, checkGroup: {}",checkitemIds,checkGroup);
        checkGroupService.edit(checkitemIds,checkGroup);
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * @Author: 位帅帅
     * @Date: 19:18 2021/1/5
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 删除检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @PostMapping("delete")
    public Result delete(Integer id) {
        log.info("[检查组-删除]id: {}",id);
        checkGroupService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * @Author: 位帅帅
     * @Date: 18:36 2021/1/6
     * @Params: []
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 查询全部检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
